package cn.jit.ssm.demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 测试Map的几种遍历方式及不同方式的耗时
 * @author: Cecurio
 * @create: 2017-12-08 20:49
 **/
public class Demo2 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 100; i++) {
            map.put("key" + i, "value" + i);
        }
        System.out.println("当前元素个数" + map.size());
        long start = System.currentTimeMillis();
        Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
        int i = 0;
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            if (i++ < 20)
                entries.remove();
        }
        System.out.print("当前元素个数" + map.size() + "\t");
        System.out.println("泛型" + (System.currentTimeMillis() - start));

        boolean isEmpty = map.isEmpty();
        start = System.currentTimeMillis();
        Set set = map.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.print("当前元素个数" + map.size() + "\t");
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        set = map.keySet();
        iterator = set.iterator();
        while (iterator.hasNext()) {
            Object obe = iterator.next();
            System.out.println(obe + "=" + map.get(obe));
        }
        System.out.print("当前元素个数" + map.size() + "\t");
        System.out.println("get" + (System.currentTimeMillis() - start));
    }
}
